#include "form_setting.h"
#include "form_welcome.h"
#include "ui_form_setting.h"

form_setting::form_setting(QWidget *parent)
    : QDialog(parent), ui(new Ui::form_setting) {
    ui->setupUi(this);
}

form_setting::~form_setting() { delete ui; }

/**
 * @brief 展示某文件的基本信息
 * @param buf 文件路径
 * @param [out] stat 存放文件大小 不存在则是-1
 * @return QString 信息
 */
QString form_setting::show_geneFileInfo(char* buf,int* stat){
    long long sz;
    QString res;
    QTextCodec* cc = QTextCodec::codecForLocale();
    sz = sizeFile(buf);
    if(sz == -1){
        res += "未能找到" + cc->toUnicode(buf) + "\n";
    }else if(sz == 0){
        res += "文件：" + cc->toUnicode(buf) + " 是空的\n";
    }else{
        res += "找到文件：" + cc->toUnicode(buf) + " 大小:" + QString::number(sz) + "字节\n";
    }
    if(stat)*stat = sz;
    return res;
}


/**
 * @brief 把信息展现在信息框中
 */
void form_setting::show_inf(){
    QString puser = ui->txt_pathUser->text();
    QString ppro = ui->txt_pathPro->text();
    QString res;

    char p1[2*FILENAME_MAX]={0},buf[2*FILENAME_MAX]={0};

    GETMBQSTR(p1,puser);        //转为char*
    char checklist[5][FILENAME_MAX] =   //检查表
        { FILENAME_STUDENTS,
            FILENAME_TEACHERS,
            FILENAME_STATISTICS,
            FILENAME_KEY,
            FILENAME_RECORDS
        };

    long long sz = -1;
    DIR* dr = opendir(p1);

    if(!dr){
        //可能是目录
        res += "未能找到用户文件夹" + puser + "\n";
        setbuf(stdout,0);
        puts(p1);
    }else{
        res += "用户文件夹：" + puser + "\n";
        for(int i = 0;i < 5;++i){
            FNAMECAT(buf,p1,checklist[i],"/");  //Qt中使用'/'分割
            res += show_geneFileInfo(buf,0);  //增添信息
        }
        closedir(dr);
    }

    //Qt风格
    QDir di(ppro);
    if(!di.exists()){
        res += "未能找到题目文件夹" + ppro + "\n";
    }else{
        res += "题目文件夹" + ppro + "\n";
        auto lst = di.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files,QDir::DirsFirst);  //目录下文件列表
        auto it = lst.begin();
        for(;it != lst.end();++it){
            //遍历...
            if(it->isDir()){
                //是文件夹
                res += "\t文件夹：" + it->fileName() + "\n";
                QDir dii(it->fileName());
                for(auto x : dii.entryInfoList(QDir::Files)){
                    //继续找
                    if(it->suffix() == "html")
                        res += "\t\tHtml文件(题目文件)：" + it->fileName() + "\n";
                }
            }else if(it->suffix() == "html"){
                res += "\tHtml文件(题目文件)：" + it->fileName() + "\n";
            }

        }
    }


    ui->txt_information->setPlainText(res);

}

/**
 * @brief 把配置存进本窗体
 */
void form_setting::set_ini(){
    //QString转宽字符串
    GETWSQSTR(2*FILENAME_MAX,this->set.problemfd,ui->txt_pathPro->text());
    GETWSQSTR(2*FILENAME_MAX,this->set.userfd,ui->txt_pathUser->text());
}

/**
 * @brief 把本窗体配置显示
 */
void form_setting::update_ini(){
    QString c;
    //宽字符串转QString
    GETQSTRWS(2*FILENAME_MAX,c,this->set.problemfd);
    ui->txt_pathPro->setText(c);
    GETQSTRWS(2*FILENAME_MAX,c,this->set.userfd);
    ui->txt_pathUser->setText(c);
}

/**
 * @brief 把自身信息也整合到主窗口中
 */
void form_setting::set_parent(){

    QString pName;
    //pName = this->parent().;

    auto pt = (Form_Welcome*)this->parentWidget();  //父窗口是form_welcome
    pt->set = this->set;
}



//=================================================================================================================================================================================================

/**
 * @brief form_setting::showEvent
 * @param qse
 */
void form_setting::showEvent(QShowEvent* qse){
    changed = 0;
}


void form_setting::on_but_pathUser_clicked()
{
    ui->txt_pathUser->setText(QFileDialog::getExistingDirectory(0,"选择文件夹"));
}


void form_setting::on_but_pathPro_clicked()
{
    ui->txt_pathPro->setText(QFileDialog::getExistingDirectory(0,"选择文件夹"));
}



void form_setting::on_txt_pathUser_textChanged(const QString &arg1)
{
    show_inf();
}


void form_setting::on_txt_pathPro_textChanged(const QString &arg1)
{
    show_inf();
}

/**
 * @brief 确认更改配置按钮
 */
void form_setting::on_buttonBox_accepted()
{
    if(QMessageBox::question(0,"更改配置文件","你确定要更改配置文件吗？继续将重新登录。") == QMessageBox::Yes){
        //先询问 确定了再改
        set_ini();                          //更改配置
        saveSettingInfo(&this->set);        //写配置文件
        checkSettingInfo(&this->set);       //检查文件完整性
        //set_parent();                       //向父窗口发送信息
        changed = 1;
    }
}

